package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override
    public Map<String, Object> list(Integer page,
                                    Integer rows,
                                    String customerName) {

        //获取库存
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Customer> customerList = customerDao.selectCustomerList(offSet, rows, customerName);

        map.put("total", String.valueOf(customerList.size()));
        map.put("rows", customerList);
        return map;

    }

    @Override
    public void saveOrUpdate(Customer customer, String customerId) {

        if (customerId == null) {
            //如果供应商id为空添加数据
            this.customerDao.saveCustomer(customer);
        } else
            this.customerDao.updateCustomer(customer, customerId);
    }

    //删除操作
    @Override
    public void delete(int id) {
        customerDao.deleteCustomer(id);
    }

    @Override
    public List<Customer> getComboboxList(String q) {
        List<Customer> comboboxList = customerDao.getComboboxList(q);
        List<Customer> collect = comboboxList.stream().filter(customer -> {
            return customer.getCustomerId().intValue() != 0;
        }).collect(Collectors.toList());
        return  collect;
    }
}